package com.kiln.juxingqs.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.global.commlib.base.BaseLoadmoreAdapter
import client.global.commlib.base.BindingViewHoder
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.ZrOrder
import com.kiln.juxingqs.databinding.LayoutItemZrPickOrderBinding
import com.kiln.juxingqs.dialog.JyInputNumDialog

class ZrPickOrderAdapter : BaseLoadmoreAdapter<ZrOrder.Order> {

    val u_type: String?

    constructor(context: Context, u_type: String?, is_detail: Int?) : super(context) {
        this.u_type = u_type
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_item_zr_pick_order,
                parent,
                false
            )
        )
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as LayoutItemZrPickOrderBinding

        val bean = getItem(position)
        binding.bean = bean
        binding.event = this

        val time = bean.create_time.substring(
            bean.create_time.length - 8,
            bean.create_time.length - 3
        )
        binding.time = time
    }

    fun confirmZhaipai(bean: ZrOrder.Order, u_type: String?) {
        if (this.u_type.equals("2")) {//撤销
            chancel(bean.id.toString())
        } else {
            JyInputNumDialog(context).apply {
                ok = { num ->
                    zhaiPai(bean.id.toString(), "${num}")
                    dismiss()
                }
            }.show()
        }

    }

    var cancelFun: ((orderId: String) -> Unit)? = null
    var zhaiPaiFun: ((orderId: String, num: String) -> Unit)? = null

    private fun zhaiPai(orderId: String, num: String) {
        zhaiPaiFun?.invoke(orderId, num)
    }

    private fun chancel(orderId: String){
        cancelFun?.invoke(orderId)
    }


}
